//
//  CenterTabBar.m
//  Anyair
//
//  Created by karlcool on 14-5-15.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "CenterTabBar.h"

@implementation CenterTabBar
@synthesize  firstBtn,secondBtn,thirdBtn,fourthBtn,delegate,backBtn,shadeBtn,centerBtn;

enum barsize{
    tabitem_width=64,
    tabitem_hight=44,
    tab_hight=46,
    tab_width=320,
    other_offtop=2,
    
    img_hight=40,
    img_width=40,
    img_x=12,
    img_y=2
    
};
- (id)initWithFrame:(CGRect)frame
{
    CGRect frame1=CGRectMake(frame.origin.x, frame.size.height-tab_hight, tab_width, tab_hight);
    
    self = [super initWithFrame:frame1];
    if (self) {
        g_selectedTag = 1;
        
        [self setBackgroundColor:[UIColor whiteColor]];
        backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:CGRectMake(0, 0, tab_width, tab_hight)];
        [backBtn setBackgroundColor:AIR_GREEN];

        UIImageView *btnImgView;
        
        
        //first
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bicon05_1"] highlightedImage:[UIImage imageNamed:@"Bicon05"]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        firstBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [firstBtn setFrame:CGRectMake(0, other_offtop, tabitem_width, tabitem_hight)];
        [firstBtn setTag:1];
        [firstBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [firstBtn addSubview:btnImgView];
        
        //second
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bicon06_1"] highlightedImage:[UIImage imageNamed:@"Bicon06"]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        secondBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [secondBtn setFrame:CGRectMake(tabitem_width, other_offtop, tabitem_width, tabitem_hight)];
        [secondBtn setTag:2];
        [secondBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [secondBtn addSubview:btnImgView];
        
        //center
        centerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [centerBtn setFrame:CGRectMake(tabitem_width*2, -18, 70, 70)];
        [centerBtn setImage:[UIImage imageNamed:@"Add"] forState:UIControlStateNormal];
        [centerBtn setImage:[UIImage imageNamed:@"Add_1"] forState:UIControlStateHighlighted];
        centerBtn.layer.cornerRadius = 35;

        [centerBtn setBackgroundColor:AIR_GREEN];
        [centerBtn setTag:3];
        [centerBtn addTarget:self action:@selector(clickCenter:) forControlEvents:UIControlEventTouchUpInside];
        //[centerBtn addSubview:btnImgView];
        
        //third
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bicon07_1"] highlightedImage:[UIImage imageNamed:@"Bicon07"]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        thirdBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [thirdBtn setFrame:CGRectMake(tabitem_width*3, other_offtop, tabitem_width, tabitem_hight)];
        [thirdBtn setTag:4];
        [thirdBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [thirdBtn addSubview:btnImgView];
        
        //fourth
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bicon08_1"] highlightedImage:[UIImage imageNamed:@"Bicon08"]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        fourthBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [fourthBtn setFrame:CGRectMake(tabitem_width*4, other_offtop, tabitem_width, tabitem_hight)];
        [fourthBtn setTag:5];
        [fourthBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [fourthBtn addSubview:btnImgView];
        
        
        [backBtn addSubview:shadeBtn];
        [backBtn addSubview:centerBtn];
        [backBtn addSubview:firstBtn];
        [backBtn addSubview:secondBtn];
        [backBtn addSubview:thirdBtn];
        [backBtn addSubview:fourthBtn];
        
        
        
        [self addSubview:backBtn];
        
        [self bringSubviewToFront:centerBtn];
        
    }
    [self buttonClickAction:firstBtn];
    return self;
}

-(void)callButtonAction:(UIButton *)sender{
    int value=sender.tag;
    if (value==1) {
        [self.delegate FirstBtnClick];
    }
    if (value==2) {
        [self.delegate SecondBtnClick];
    }
    if (value==4) {
        [self.delegate ThirdBtnClick];
    }
    if (value==5) {
        [self.delegate FourthBtnClick];
    }
    
}

- (void)clickCenter:(UIButton*)sender {
    [self.delegate CenterBtnClick];
}

- (void)clickShare:(UIButton*)sender {
    [self.delegate SecondBtnClick];
}

-(void)buttonClickAction:(id)sender{
    UIButton *btn=(UIButton *)sender;
    g_selectedTag=btn.tag;

    if (firstBtn.tag!=btn.tag&&btn!=secondBtn) {
        ((UIImageView *)firstBtn.subviews[0]).highlighted=NO;
    }
    
    
    if (thirdBtn.tag!=btn.tag&&btn!=secondBtn) {
        
        ((UIImageView *)thirdBtn.subviews[0]).highlighted=NO;
    }
    
    if (fourthBtn.tag!=btn.tag&&btn!=secondBtn) {
        
        ((UIImageView *)fourthBtn.subviews[0]).highlighted=NO;
    }
    
    
    [self imgAnimate:btn];
    
    if (btn!=centerBtn) {
        ((UIImageView *)btn.subviews[0]).highlighted=YES;
    }
    if (btn==secondBtn) {
        ((UIImageView *)btn.subviews[0]).highlighted=NO;
    }
    
    [self callButtonAction:btn];
    
    return;
 
}

- (void)moveShadeBtn:(UIButton*)btn{
    
    [UIView animateWithDuration:0.3 animations:
     ^(void){
         
         CGRect frame = shadeBtn.frame;
         frame.origin.x = btn.frame.origin.x;
         shadeBtn.frame = frame;
         
         
     } completion:^(BOOL finished){//do other thing
     }];
    
    
}

- (void)imgAnimate:(UIButton*)btn{
    
    if (btn!=centerBtn) {
        UIView *view=btn.subviews[0];
        
        [UIView animateWithDuration:0.1 animations:
         ^(void){
             
             view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.5, 0.5);
             
             
         } completion:^(BOOL finished){//do other thing
             [UIView animateWithDuration:0.2 animations:
              ^(void){
                  
                  view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.2, 1.2);
                  
              } completion:^(BOOL finished){//do other thing
                  [UIView animateWithDuration:0.1 animations:
                   ^(void){
                       
                       view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1);
                       
                       
                   } completion:^(BOOL finished){//do other thing
                   }];
              }];
         }];
    }
}

@end
